package com.tigerhhzz.smail.common;

import com.aliyuncs.exceptions.ClientException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 短信监听类
 */
@Slf4j
@Component
@RabbitListener(queues = "sms")
public class SmsListener {
 
    @Autowired
    private SmsUtils smsUtils;
 
    @Value("${aliyun.sms.templateCode}")
    private String template_code;//模板编号
 
    @Value("${aliyun.sms.signName}")
    private String sign_name;//签名
 
    @RabbitHandler
    public void sendSms(Map<String,String> map){
        log.info("手机号："+map.get("mobile"));
        log.info("验证码："+map.get("code"));
        try {
            // 模板格式根据自己的需求来做
            String param = "{\"name\":\"Ron\", \"code\":\"" + map.get("code") + "\"}";
            smsUtils.sendSms(map.get("mobile"),sign_name,template_code, param);
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}